<?php
class Admin_DimissionController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pDid = empty($p['d_id'])?'':Tool_Fnc::safe_string($p['d_id']);
        $pKeyword = empty($p['keyword'])?'':Tool_Fnc::safe_string($p['keyword']);
        $pDimissiondate = empty($p['dimission_date'])?'':Tool_Fnc::safe_string($p['dimission_date']);
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        
        $tDIMO = new DepartmentinfoModel;
        $tWMO = new WorkerModel;
        $tDMO = new DimissionModel;
        $tAMO = new AdminModel;

        $tWhere = ' 1 ';

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){//管理员之外
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and w.department = '.$tAData[0]['department'];
            }
        }

        if(!empty($pKeyword)){
            $tWhere .= ' and w.phone like \'%'.$pKeyword.'%\' or w.name like \'%'.$pKeyword.'%\'';
        }
        if(!empty($pDimissiondate)){
            $tWhere .= ' and d.dimission_date like \'%'.$pDimissiondate.'%\'';
        }
        if(!empty($pDepartment)){
            $tWhere .= ' and w.department = '.$pDepartment;
        }
        
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pDepartmentID = empty($p['department_id'])?Tool_Fnc::ajaxMsg('系统错误'):Tool_Fnc::safe_string($p['department_id']);
            $tSql = 'select id,name from '.$tWMO->table.' where department = '.$pDepartmentID;
            $tDatas = $tWMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }


        //所在部门
        $tDDatas = $tDIMO->field('num,introduce')->fList();

        $tSql = 'select count(*) c from '.$tDMO->table.' d left join '.$tWMO->table.' w on w.id = d.w_id left join '.$tDIMO->table.' di on di.num = w.department where '.$tWhere;
        $tCount = $tDMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tSql = 'select d.id id,w.name name,w.phone phone,di.introduce department,d.dimission_date dimission_date,d.dimission_reason dimission_reason,d.financial_settlement financial_settlement,d.return_goods return_goods,d.remarks remarks from '.$tDMO->table.' d left join '.$tWMO->table.' w on w.id = d.w_id left join '.$tDIMO->table.' di on di.num = w.department where '.$tWhere.' order by d.id desc limit '.$tPage->limit();
            $tDatas = $tDMO->query($tSql);
        }

        if(!empty($pDid)){//编辑页面显示
            //$tTData = $tTMO->field('*')->where('id = '.$pTid)->fRow();    
            $tSql = 'select d.*,w.department department,w.name name from '.$tDMO->table.' d left join '.$tWMO->table.' w on d.w_id = w.id where d.id = '.$pDid;
            $tDData = $tDMO->query($tSql);
            Tool_Fnc::ajaxMsg('',1,$tDData[0]);
        }

        $this->assign('pKeyword',$pKeyword);
        $this->assign('pDimissiondate',$pDimissiondate);
        $this->assign('pDepartment',$pDepartment);
        $this->assign('tDatas',$tDatas);
        $this->assign('tCaiwu',$tDMO->tCaiwu);
        $this->assign('tReturngoods',$tDMO->tReturngoods);
        $this->assign('tDDatas',$tDDatas);
    }
    public function addAction(){
        $p = $_REQUEST;

        $tDMO = new DimissionModel;
        $tWSMO = new WorkerseniorModel;
        $tSWMO = new SupportworkerModel;
        $tWMO = new WorkerModel;

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pSupportworker = empty($p['supportworker'])?Tool_Fnc::ajaxMsg('护工ID不能为空'):Tool_Fnc::safe_string($p['supportworker']);
            $pDimissiondate = empty($p['dimission_date'])?Tool_Fnc::ajaxMsg('离职时间不能为空'):Tool_Fnc::safe_string($p['dimission_date']);
            $pFinancialsettlement = empty($p['financial_settlement'])?Tool_Fnc::ajaxMsg('财务计算不能为空'):Tool_Fnc::safe_string($p['financial_settlement']);
            $pReturngoods = empty($p['return_goods'])?Tool_Fnc::ajaxMsg('物品归还不能为空'):Tool_Fnc::safe_string($p['return_goods']); 
            $pDimissionreason = empty($p['dimission_reason'])?Tool_Fnc::ajaxMsg('原因不能为空'):Tool_Fnc::safe_string($p['dimission_reason']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);
            
            $tTime = time();

            $tData = array(
                'w_id' => $pSupportworker,
                'dimission_date' => $pDimissiondate,
                'financial_settlement' => $pFinancialsettlement,
                'return_goods' => $pReturngoods,
                'dimission_reason' => $pDimissionreason,
                'remarks' => $pRemarks,
                'created' => $tTime,
            );
            $tDMO->begin();
            if(!$tId = $tDMO->insert($tData)){
                Tool_Fnc::ajaxMsg('离职失败001');
            }    
            //查询护工账号ID
            $tSwID = $tSWMO->field('id')->where('w_id = '.$pSupportworker)->fRow();

            //解绑老人
            $tWSDatas = $tWSMO->field('id')->where('sw_id = '.$tSwID['id'])->fList();
            if(!empty($tWSDatas)){
                $tSql = 'delete from '.$tWSMO->table.' where sw_id = '.$tSwID;
                if(!$tWSMO->exec($tSql)){
                    $tDMO->back();
                    Tool_Fnc::ajaxMsg('离职失败002');
                }
            }
            //删除分配的账号记录及更改员工表当前状态
            if(!empty($tSwID)){
                $tSql = 'delete from '.$tSWMO->table.' where w_id = '.$pSupportworker;
                if(!$tSWMO->exec($tSql)){
                    $tDMO->back();
                    Tool_Fnc::ajaxMsg('离职失败003');
                }
            }
            if(!empty($pSupportworker)){
                $tData = array(
                    'id' => $pSupportworker,
                    'status' => 2,    
                );    
                if(!$tWMO->update($tData)){
                    $tDMO->back();
                    Tool_Fnc::ajaxMsg('离职失败004');
                }
            }
            $tDMO->commit();
            Tool_Fnc::ajaxMsg('离职成功');
        }
    
    }
    public function editAction(){
        $p = $_REQUEST;

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pTid = empty($p['tId'])?Tool_Fnc::ajaxMsg('tId不能为空'):Tool_Fnc::safe_string($p['tId']);    
            $pSupportworker = empty($p['supportworker'])?Tool_Fnc::ajaxMsg('护工ID不能为空'):Tool_Fnc::safe_string($p['supportworker']);
            $pDimissiondate = empty($p['dimission_date'])?Tool_Fnc::ajaxMsg('离职时间不能为空'):Tool_Fnc::safe_string($p['dimission_date']);
            $pFinancialsettlement = empty($p['financial_settlement'])?Tool_Fnc::ajaxMsg('财务计算不能为空'):Tool_Fnc::safe_string($p['financial_settlement']);
            $pReturngoods = empty($p['return_goods'])?Tool_Fnc::ajaxMsg('物品归还不能为空'):Tool_Fnc::safe_string($p['return_goods']); 
            $pDimissionreason = empty($p['dimission_reason'])?Tool_Fnc::ajaxMsg('原因不能为空'):Tool_Fnc::safe_string($p['dimission_reason']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);
            
            $tTime = time();

            $tData = array(
                'w_id' => $pSupportworker,
                'dimission_date' => $pDimissiondate,
                'financial_settlement' => $pFinancialsettlement,
                'return_goods' => $pReturngoods,
                'dimission_reason' => $pDimissionreason,
                'remarks' => $pRemarks,
                'created' => $tTime,
                'id' => $pTid,
            );

            $tDMO = new DimissionModel;

            if(!$tDMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }    
            Tool_Fnc::ajaxMsg('修改成功');
        }
    
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);
        
        $tDMO = new DimissionModel;

        $tSql = 'delete from '.$tDMO->table.' where id = '.$pId;
        if(!$tDMO->exec($tSql)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    }
}
?>
